import entity.Group;
import entity.GroupMessage;
import enums.ResponseType;
import request.group.GroupCreateRequest;
import request.group.message.GroupMessageListRequest;
import request.group.relation.GroupRelationBuildRequest;
import request.message.MessageListRequest;
import request.relation.FriendAddRequest;
import response.ErrorResponse;
import response.Response;
import response.group.GroupCreateResponse;
import response.group.message.GroupMessageListResponse;
import response.group.relation.GroupRelationBuildResponse;
import response.message.MessageListResponse;
import response.relation.FriendAddResponse;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ContactsHandler implements ActionListener {
    private ContactsView contactsView;

    public ContactsHandler(ContactsView contactsView) {
        this.contactsView = contactsView;
    }

    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("发起聊天".equals(text)) {
            chat(contactsView.getUserName(), contactsView.getGuestName());
        } else if ("添加新好友".equals(text)) {
            addFriend(contactsView.getUserName(), contactsView.getFriendName());
        } else if ("查看好友请求".equals(text)) {
            new FriendApplicationView(contactsView.getUserName());
        } else if("创建群聊".equals(text)){
            createGroup(contactsView.getUserName(),contactsView.getGroupFriendName(),contactsView.getAddGroupName());
        } else if("进入群聊".equals(text)){
            enterGroup(contactsView.getUserName(),contactsView.getGroupid());
        }
    }

    public void chat(String hostUserName, String guestUserName) {
        MessageListRequest messageListRequest = new MessageListRequest(hostUserName, guestUserName);
        Response response = ConnectionUtil.makeConnection(messageListRequest);
        if (response.getType().equals(ResponseType.MessageList)) {
            MessageListResponse messageListResponse = (MessageListResponse) response;
            new ChatView(hostUserName, guestUserName, messageListResponse.getMessageList());
        }
        else if(response.getType().equals(ResponseType.Error)) {
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void addFriend(String hostUserName, String guestUserName) {
        FriendAddRequest friendAddRequest = new FriendAddRequest(hostUserName, guestUserName);
        Response response = ConnectionUtil.makeConnection(friendAddRequest);
        if (response.getType() == ResponseType.FriendAdd) {
            FriendAddResponse friendAddResponse = (FriendAddResponse) response;
                JOptionPane.showMessageDialog(contactsView, "已发送好友申请");
        }
        else if(response.getType().equals(ResponseType.Error)) {
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void createGroup(String hostUserName,String friendUserName,String groupName){
        Group group=new Group(groupName,hostUserName,null);
        GroupCreateRequest groupCreateRequest=new GroupCreateRequest(group);
        Response response1=ConnectionUtil.makeConnection(groupCreateRequest);
        if(response1.getType()==ResponseType.GroupCreate){
            GroupCreateResponse groupCreateResponse=(GroupCreateResponse) response1;
        }
        else if(response1.getType().equals(ResponseType.Error)) {
            ErrorResponse errorResponse=(ErrorResponse) response1;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
            return;
        }
        GroupRelationBuildRequest groupRelationBuildRequest=new GroupRelationBuildRequest(friendUserName,group.getUuid());
        Response response2=ConnectionUtil.makeConnection(groupRelationBuildRequest);
        if(response2.getType()==ResponseType.GroupRelationBuild){
            GroupRelationBuildResponse groupRelationBuildResponse=(GroupRelationBuildResponse) response2;
        }
        else if(response2.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response2;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
            return;
        }
        GroupRelationBuildRequest groupRelationBuildRequest1=new GroupRelationBuildRequest(hostUserName,group.getUuid());
        Response response3=ConnectionUtil.makeConnection(groupRelationBuildRequest1);
        if(response3.getType()==ResponseType.GroupRelationBuild){
            GroupRelationBuildResponse groupRelationBuildResponse=(GroupRelationBuildResponse) response3;
            JOptionPane.showMessageDialog(contactsView, "创建群聊成功！");
        }
        else if(response3.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response3;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
        }
    }

    public void enterGroup(String hostUserName,UUID groupId){
        List<GroupMessage>messageHistory=new ArrayList<>();
        GroupMessageListRequest groupMessageListRequest=new GroupMessageListRequest(groupId);
        Response response=ConnectionUtil.makeConnection(groupMessageListRequest);
        if(response.getType()==ResponseType.GroupMessageList){
            GroupMessageListResponse groupMessageListResponse=(GroupMessageListResponse) response;
            messageHistory=groupMessageListResponse.getGroupMessageList();
        }
        else if(response.getType().equals(ResponseType.Error)){
            ErrorResponse errorResponse=(ErrorResponse) response;
            JOptionPane.showMessageDialog(contactsView, errorResponse.getResponseErrorCode().getMessage());
        }
        new GroupChatView(hostUserName,messageHistory,groupId);
    }
}
